In many statically typed object-oriented languages, arrays are covariant. This means that a value of type T[ ] can be passed/assigned to a value of type S[ ] whenever there is an implicit reference conversion from type T to type S. Assuming an implicit reference conversion from string to object exists, the following array assignment is valid:
string[ ] ss=new string[ ] {“Hello”, “World”};
object[ ] os=ss;
While this is extremely convenient and intuitive for programmers, it requires that each write-access of a value x to a covariant array xs performs a dynamic type-check to ensure that there is an implicit reference conversion from the dynamic type of the value x to the dynamic element type of the array xs. Without this runtime check it would be possible to create an array whose values do not conform to the expected type of the array resulting in potentially unsafe and/or unknown consequences.
Continuing with this example, it is incorrect to insert a Button element into the array os, since that would mean that the array ss now has both string and Button elements (since ss and os both point to the same array instance). The following assignment thus throws a run-time exception:
os[0]=new Button( );
string s=ss[0];
Before the advent of generics, arrays were the only “generic types” available. Generic types form a powerful and ubiquitous aspect of object-oriented programming. Generic types, also known as parameterized types, define formal constructs of some type(s) (e.g., a list with elements of some type, a queue with elements of some type, a stack with elements of some type . . . ). This “some type(s)” is referred to in the art as the parameter(s) of generic types. A common notation for generic types is List<T>, where T represents the type parameter and List is the generic type. When a concrete type is used as a type parameter of a generic type, the resulting type is commonly referred to as generic instantiation or constructed type (e.g., a stack of integer values Stack<int>, list of strings List<string>, queue of floating-point values Queue<float> . . . ). Many programming languages (e.g., C# and Java) require generic type to be invariant which prohibits, for example, passing a List<string> where a List<object> is expected.